from gui.mainDlg import Ui_matchMain
from gui.colorCorrectDlg import Ui_Form_colorCorrect
from gui.colorCombineDlg import Ui_Form_colorCombine
from gui.evaluateDlg import Ui_Form_evaluate
from gui.configDlg import Ui_Form_configDlg

from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import QApplication, QDialog, QFileDialog, QListWidgetItem, QMessageBox
import sys
import os
import xml.etree.ElementTree as ET

from core.config import Config
from manager.TierManager import TierManager

def stsSmpSetSlot():
    msgBox = QMessageBox()
    msgBox.information(None, "tip", "设置标样完成！", msgBox.Yes, msgBox.Yes)

def dataSetSlot():
    msgBox = QMessageBox()
    msgBox.information(None, "tip", "设置光谱数据完成！", msgBox.Yes, msgBox.Yes)

def msgSlot(title, msg):
    msgBox = QMessageBox()
    if title == "提示":
        msgBox.information(None, title, msg, msgBox.Yes, msgBox.Yes)
    elif title == "警告":
        msgBox.warning(None, title, msg, msgBox.Yes, msgBox.Yes)
    elif title == "错误":
        msgBox.critical(None, title, msg, msgBox.Yes, msgBox.Yes)

class MyDialog(QDialog):
    myClose = QtCore.pyqtSignal()
    def closeEvent(self, a0: QtGui.QCloseEvent) -> None:
        self.myClose.emit()

class UIManager(object):
    """
    UI管理层；
    """
    def __init__(self):
        self.cfg = Config()
        self.tierManager = TierManager(self.cfg)
        # 样本光谱设置 信号；
        self.tierManager.stdSmpSetSignal.connect(stsSmpSetSlot)
        self.tierManager.dataSmpSetSignal.connect(dataSetSlot)
        # 配方， 修色 信号；
        self.tierManager.colorCombine.colorCombineSignal.connect(self.__updateColorCombineResult)               # 配色完成；
        self.tierManager.colorCombine.colorCorrectSignal.connect(self.__updateColorCorrectResult)
        self.tierManager.colorCombine.colorCombineStepSignal.connect(self.__updateColorCombineProgress)
        self.tierManager.colorCombine.colorCorrectStepSignal.connect(self.__updateColorCorrectProgress)

        #　三个窗体；
        self.uiMain = None
        self.colorCombineDlg = None
        self.colorCorrectDlg = None

    def __saveXml(self):
        """
        关闭时，保存数据；
        :return:
        """
        xmlpath = os.path.abspath(".") + "/set.xml"
        if not os.path.exists(xmlpath):
            self.__initXml()
            return
        tree = ET.parse(xmlpath)
        root = tree.getroot()
        path = root.find("path")
        try:
            stdFilePathNode = path.find("stdFile")
            stdFilePathNode.text = self.uiMain.lineEdit_stdSmp.text().replace("\\", "/")
            blackFileNode = path.find("blackFile")
            blackFileNode.text = self.uiMain.lineEdit_black_ref_curve.text().replace("\\", "/")
            caliFileNode = path.find("caliFile")
            caliFileNode.text = self.uiMain.lineEdit_std_ref_curve.text().replace("\\", "/")
            dataFileNode = path.find("dataFile")
            dataFileNode.text = self.uiMain.lineEdit_ref_curve.text().replace("\\", "/")
            path.find("recipeFile").text = self.cfg.recipeFilePath.replace("\\", "/")
        except:
            print("有部分为空的")

        cfgNode = root.find("cfg")
        cfgNode.find("ref400Offset").text = "%d" % self.cfg.ref400Offset
        cfgNode.find("sampleNum").text = "%d" % self.cfg.sampleNum
        cfgNode.find("sampleStartRow").text = "%d" % self.cfg.sampleStartRow
        cfgNode.find("whiteBlackBaseRow").text = "%d" % self.cfg.whiteBlackBaseRow
        cfgNode.find("baseWhiteRow").text = "%d" % self.cfg.baseWhiteRow
        cfgNode.find("pureRow").text = "%d" % self.cfg.pureRow
        cfgNode.find("nColorant").text = "%d" % self.cfg.nColorant
        cfgNode.find("weight").text = "%.2f" % self.cfg.weight
        conc = ""
        for i in range(len(self.cfg.testConc)):
            conc += "%.4f" % self.cfg.testConc[i]
            if i != len(self.cfg.testConc) - 1:
                conc += ","
        cfgNode.find("conc").text = conc

        tree.write(xmlpath)

    def __initXml(self):
        xmlpath = os.path.abspath(".") + "/set.xml"
        if os.path.exists(xmlpath):                 # 存在，就获取；
            tree = ET.parse(xmlpath)
            rootNode = tree.getroot()
            path = rootNode.find("path")
            stdFilePath = path.find("stdFile").text
            blackFilePath = path.find("blackFile").text
            caliFilePath = path.find("caliFile").text
            recipeFilePath = path.find("recipeFile").text
            dataFilePath = path.find("dataFile").text

            # 首先加载标样；
            try:
                if os.path.exists(stdFilePath):
                    self.tierManager.setStdSmp(stdFilePath)
                    self.uiMain.lineEdit_stdSmp.setText(stdFilePath)
            except:
                print("标样设置失败")

            # 再加载纯黑, 标定;
            try:
                if blackFilePath and os.path.exists(blackFilePath):
                    self.cfg.blackRefFilePath = blackFilePath
                    self.uiMain.lineEdit_black_ref_curve.setText(blackFilePath)
                if caliFilePath and os.path.exists(caliFilePath):
                    self.cfg.calibrationFilePath = caliFilePath
                    self.uiMain.lineEdit_std_ref_curve.setText(caliFilePath)
                if recipeFilePath and os.path.exists(recipeFilePath):
                    self.cfg.recipeFilePath = recipeFilePath
            except:
                pass

            # 加载数据；
            try:
                if dataFilePath and os.path.exists(dataFilePath):
                    self.tierManager.setDataFile(dataFilePath)
                    self.uiMain.lineEdit_ref_curve.setText(dataFilePath)
                    self.__listUpdate()
            except:
                print("光谱数据加载失败")

            cfgNode = rootNode.find("cfg")
            try:
                self.cfg.ref400Offset = int(cfgNode.find("ref400Offset").text)
                self.cfg.sampleNum = int(cfgNode.find("sampleNum").text)
                self.cfg.sampleStartRow = int(cfgNode.find("sampleStartRow").text)
                self.cfg.whiteBlackBaseRow = int(cfgNode.find("whiteBlackBaseRow").text)
                self.cfg.baseWhiteRow = int(cfgNode.find("baseWhiteRow").text)
                self.cfg.pureRow = int(cfgNode.find("pureRow").text)
                self.cfg.nColorant = int(cfgNode.find("nColorant").text)
                self.cfg.weight = float(cfgNode.find("weight").text)
                tmpConc = cfgNode.find("conc").text
                allConc = tmpConc.split(",")
                for i in range(len(allConc)):
                    self.cfg.testConc[i] = float(allConc[i])
            except:
                print("有设置不存在")
            return
        # 创建；
        root = ET.Element("root")
        pathArea = ET.SubElement(root, "path")
        stdFile = ET.SubElement(pathArea, "stdFile")
        stdFile.text = os.path.abspath(".").replace("\\", "/") + "/standard.xlsx"

        stdBlackFile = ET.SubElement(pathArea, "blackFile")
        stdBlackFile.text = os.path.abspath(".").replace("\\", "/") + "/纯黑_SCE.xlsx"

        dataFile = ET.SubElement(pathArea, "dataFile")
        dataFile.text = os.path.abspath(".").replace("\\", "/") + "/sample.xlsx"

        caliFile = ET.SubElement(pathArea, "caliFile")
        caliFile.text = os.path.abspath(".").replace("\\", "/") + "/标定.xlsx"

        recipeFile = ET.SubElement(pathArea, "recipeFile")
        recipeFile.text = ""

        # 配置；
        cfgArea =  ET.SubElement(root, "cfg")
        ref400Offset = ET.SubElement(cfgArea, "ref400Offset")
        ref400Offset.text = "%d" % self.cfg.ref400Offset
        sampleNum = ET.SubElement(cfgArea, "sampleNum")
        sampleNum.text = "%d" % self.cfg.sampleNum
        sampleStartRow = ET.SubElement(cfgArea, "sampleStartRow")
        sampleStartRow.text = "%d" % self.cfg.sampleStartRow
        whiteBlackBaseRow = ET.SubElement(cfgArea, "whiteBlackBaseRow")
        whiteBlackBaseRow.text = "%d" % self.cfg.whiteBlackBaseRow
        baseWhiteRow = ET.SubElement(cfgArea, "baseWhiteRow")
        baseWhiteRow.text = "%d" % self.cfg.baseWhiteRow
        pureRow = ET.SubElement(cfgArea, "pureRow")
        pureRow.text = "%d" % self.cfg.pureRow
        nColorant = ET.SubElement(cfgArea, "nColorant")
        nColorant.text = "%d" % self.cfg.nColorant
        ET.SubElement(cfgArea, "weight").text = "%.2f" % self.cfg.weight
        # 初始化；
        conc = ""
        for i in range(len(self.cfg.testConc)):
            conc += "%.4f" % self.cfg.testConc[i]
            if i != len(self.cfg.testConc) - 1:
                conc += ","
        tmpConc = ET.SubElement(cfgArea, "conc")
        tmpConc.text = conc

        tree = ET.ElementTree(root)
        tree.write(xmlpath,'utf-8',True)


    ################################################################################################################
    # 配色， 修色 信号回调函数；
    ################################################################################################################
    def __updateColorCombineProgress(self, iter, val, conc):
        """
        更新迭代次数以及当前色差；
        :param iter:
        :param val:
        :return:
        """
        if self.colorCombineDlg:
            self.colorCombineDlg.label_iter.setText("%d" % iter)
            self.colorCombineDlg.label_colorError.setText("%.4f" % val)
            qualityInput = [self.colorCombineDlg.lineEdit_quality1, self.colorCombineDlg.lineEdit_quality2, self.colorCombineDlg.lineEdit_quality3,
                            self.colorCombineDlg.lineEdit_quality4, self.colorCombineDlg.lineEdit_quality5, self.colorCombineDlg.lineEdit_quality6]
            for i in range(len(conc)):
                qualityInput[i].setText("%.4f" % conc[i])

    def __updateColorCorrectProgress(self, iter, val, conc, addConc):
        """
        更新修色进程；
        :param iter:
        :param val:
        :param conc:
        :param addConc:
        :return:
        """
        if self.colorCorrectDlg:
            self.colorCorrectDlg.label_iter.setText("%d" % iter)
            self.colorCorrectDlg.label_colorError.setText("%.4f" % val)

            recipeInput = [self.colorCorrectDlg.lineEdit_recipe1, self.colorCorrectDlg.lineEdit_recipe2, self.colorCorrectDlg.lineEdit_recipe3, self.colorCorrectDlg.lineEdit_recipe4,
                           self.colorCorrectDlg.lineEdit_recipe5, self.colorCorrectDlg.lineEdit_recipe6]

            addInput = [self.colorCorrectDlg.lineEdit_add1, self.colorCorrectDlg.lineEdit_add2, self.colorCorrectDlg.lineEdit_add3,
                        self.colorCorrectDlg.lineEdit_add4, self.colorCorrectDlg.lineEdit_add5, self.colorCorrectDlg.lineEdit_add6]
            for i in range(len(conc)):
                recipeInput[i].setText("%.4f" % conc[i])
            for i in range(len(addConc)):
                addInput[i].setText("%.4f" % addConc[i])

    def __updateColorCombineResult(self):
        """
        更新配色结果；
        :return:
        """
        msgSlot("提示", "配色完成")
        self.tierManager.colorCombineFinished.emit()

    def __updateColorCorrectResult(self):
        """
        更新修色结果；
        :return:
        """
        msgSlot("提示", "修色完成")
        self.tierManager.colorCorrectFinished.emit()

    ################################################################################################################
    # 主界面；
    ################################################################################################################
    def showMain(self):
        app = QApplication(sys.argv)
        md = MyDialog()
        ui = Ui_matchMain()
        ui.setupUi(md)
        md.setWindowTitle("油漆临时配色软件")
        md.show()
        self.uiMain = ui

        # 信号；
        ui.pushButton_setting.clicked.connect(self.showSettingDlg)
        ui.pushButton_colorCombine.clicked.connect(self.showColorCombineDlg)
        ui.pushButton_colorCorrect.clicked.connect(self.showColorCorrectDlg)
        ui.pushButton_evaluate.clicked.connect(self.showEvaluateDlg)
        ui.pushButton_open_std.clicked.connect(lambda: self.openStdSmp(ui.lineEdit_stdSmp))
        ui.pushButton_open_ref.clicked.connect(lambda: self.openRefSmp(ui.lineEdit_ref_curve))
        ui.pushButton_open_black_ref.clicked.connect(lambda: self.openBlackRefSample(ui.lineEdit_black_ref_curve))
        ui.pushButton_open_std_ref.clicked.connect(lambda: self.openCalibrationRefSample(ui.lineEdit_std_ref_curve))

        self.__listUpdate()

        ui.lineEdit_ref_curve.setEnabled(False)
        ui.lineEdit_stdSmp.setEnabled(False)
        ui.lineEdit_black_ref_curve.setEnabled(False)
        ui.lineEdit_std_ref_curve.setEnabled(False)
        self.__initXml()
        md.myClose.connect(self.__saveXml)
        sys.exit(app.exec_())

    def __listUpdate(self):
        self.uiMain.listWidget_colorant.clear()
        nColorants = len(self.tierManager.group.colorants)
        for i in range(nColorants):
            item = QListWidgetItem()
            item.setText(self.tierManager.group.colorants[i].colorantType)
            item.setCheckState(False)
            self.uiMain.listWidget_colorant.addItem(item)

    ################################################################################################################
    # 标样， 光谱数据， 配方打样样本
    ################################################################################################################
    def openStdSmp(self, lineEdit):
        """
        打开标样数据文件；
        :param lineEdit:
        :return:
        """
        fileName = QFileDialog.getOpenFileName(None, "", "." , "Excel Files(*.xlsx);;")
        if fileName[0]:
            lineEdit.setText(fileName[0])
            try:
                self.tierManager.setStdSmp(fileName[0])
            except:
                lineEdit.setText("")
                msgSlot("错误", "标样文件格式不正确， 请确定")

    def openRefSmp(self, lineEdit):
        """
        打开光谱数据文件；
        :param lineEdit:
        :return:
        """
        fileName = QFileDialog.getOpenFileName(None, "", ".", "Excel Files(*.xlsx);;")
        if fileName[0]:
            lineEdit.setText(fileName[0])
            try:
                self.tierManager.setDataFile(fileName[0])
            except:
                lineEdit.setText("")
                msgSlot("错误", "光谱数据文件格式不正确")
                return
            # 给listWidget赋值；
            self.uiMain.listWidget_colorant.clear()
            nColorants = len(self.tierManager.group.colorants)
            for i in range(nColorants):
                item = QListWidgetItem()
                item.setText(self.tierManager.group.colorants[i].colorantType)
                item.setCheckState(False)
                self.uiMain.listWidget_colorant.addItem(item)

    def openRecipeSample(self, lineEdit):
        fileName = QFileDialog.getOpenFileName(None, "", ".", "Excel Files(*.xlsx);;")
        if fileName[0]:
            lineEdit.setText(fileName[0])
            try:
                self.tierManager.recipeSmp.loadSmp(fileName[0])
                self.cfg.recipeFilePath = fileName[0]
            except:
                lineEdit.setText("")
                msgSlot("错误", "样本格式不正确， 请确定")
                return

    def openBlackRefSample(self, lineEdit):
        fileName = QFileDialog.getOpenFileName(None, "", ".", "Excel Files(*.xlsx);;")
        if fileName[0]:
            lineEdit.setText(fileName[0])
            self.cfg.blackRefFilePath = fileName[0]

    def openCalibrationRefSample(self, lineEdit):
        fileName = QFileDialog.getOpenFileName(None, "", ".", "Excel Files(*.xlsx);;")
        if fileName[0]:
            lineEdit.setText(fileName[0])
            self.cfg.calibrationFilePath = fileName[0]
    ################################################################################################################
    # 设置窗体；
    ################################################################################################################
    def showSettingDlg(self):
        md = QDialog()
        ui = Ui_Form_configDlg()
        ui.setupUi(md)
        md.setWindowTitle("配置")
        ui.lineEdit_400ref.setText("%d" % self.cfg.ref400Offset)
        ui.lineEdit_baseWhiteRow.setText("%d" % self.cfg.baseWhiteRow)
        ui.lineEdit_colorant_num.setText("%d" % self.cfg.nColorant)
        ui.lineEdit_pure_row.setText("%d" % self.cfg.pureRow)
        ui.lineEdit_step_row.setText("%d" % self.cfg.sampleStartRow)
        ui.lineEdit_wbBaseRow.setText("%d" % self.cfg.whiteBlackBaseRow)
        ui.lineEdit_stepSamp_Num.setText("%d" % self.cfg.sampleNum)
        ui.lineEdit_weight.setText("%.2f" % self.cfg.weight)

        ui.pushButton_cancel.clicked.connect(lambda: self.__settingDlgCancel(md))
        ui.pushButton_confirm.clicked.connect(lambda: self.__settingDlgConfirm(ui, md))

        md.exec()

    def __settingDlgCancel(self, md):
        md.close()

    def __settingDlgConfirm(self, ui, md):
        self.cfg.ref400Offset =  int(ui.lineEdit_400ref.text())
        self.cfg.sampleNum = int(ui.lineEdit_stepSamp_Num.text())
        self.cfg.baseWhiteRow = int(ui.lineEdit_baseWhiteRow.text())
        self.cfg.nColorant = int(ui.lineEdit_colorant_num.text())
        self.cfg.pureRow = int(ui.lineEdit_pure_row.text())
        self.cfg.sampleStartRow = int(ui.lineEdit_step_row.text())
        self.cfg.whiteBlackBaseRow = int(ui.lineEdit_wbBaseRow.text())
        self.cfg.weight = float(ui.lineEdit_weight.text())
        md.close()

    ################################################################################################################
    # 配色窗体；
    ################################################################################################################
    def showColorCombineDlg(self):
        md = QDialog()
        ui = Ui_Form_colorCombine()
        ui.setupUi(md)
        md.setWindowTitle("配色计算")
        # ui.pushButton_match.clicked.connect()                   # 配色；

        self.__isSetStdSmpAndColorant()
        self.colorCombineDlg = ui

        idxList = self.getSelectColorant()
        bSelectRight = self.tierManager.selectColorant(idxList)
        if not bSelectRight:
            self.colorCombineDlg = None
            msgSlot("错误", "请选择三个色浆")
            return

        # 色浆设置；
        nCompose =  self.tierManager.formula.nComp
        colorantInput = [ui.lineEdit_colorant1, ui.lineEdit_colorant2, ui.lineEdit_colorant3,
                         ui.lineEdit_colorant4, ui.lineEdit_colorant5, ui.lineEdit_colorant6]

        for i in range(nCompose):
            colorantInput[i].setText("%s" % self.tierManager.formula.colorantsComp[i].colorants.colorantType)

        ui.pushButton_match.clicked.connect(lambda: self.__colorCombineMatch(ui))           # 点击“配色”按钮；
        ui.pushButton_show.clicked.connect(lambda: self.__colorCombineShow(ui))             # 点击显示光谱对比；

        md.exec()
        self.colorCombineDlg = None

    def __colorCombineShow(self, ui):
        """
        点击“显示光谱对比”
        :param ui:
        :return:
        """
        testConc = []
        for i in range(self.tierManager.colorCombine.formula.nComp):
            testConc.append(self.tierManager.colorCombine.formula.colorantsComp[i].quality)
        self.tierManager.calcRef(testConc, self.tierManager.group.baseWhite)
        self.tierManager.plotRefCurve()

    def __colorCombineMatch(self, ui):
        """
        点击配色；
        :param ui:
        :return:
        """
        # 首先对配方的成分进行含量设定；
        qualityInput = [ui.lineEdit_quality1, ui.lineEdit_quality2, ui.lineEdit_quality3, ui.lineEdit_quality4,
                        ui.lineEdit_quality5, ui.lineEdit_quality6]
        nCompose = self.tierManager.formula.nComp

        ui.pushButton_match.setEnabled(False)
        self.tierManager.colorCombineFinished.connect(lambda: self.__enableBtn(ui.pushButton_match))

        # 进行配色； TODO:
        self.tierManager.startColorCombine()
        # assert len(testConc) == nCompose
        # # 显示色差；
        # for i in range(nCompose):
        #     qualityInput[i].setText("%.4f" % testConc[i])
        # self.tierManager.calcRef(testConc, self.tierManager.group.baseWhite)
        # smp = self.tierManager.calcDeltaE()
        # ui.label_dl.setText("%.4f" % smp.deltaLab[0])
        # ui.label_da.setText("%.4f" % smp.deltaLab[1])
        # ui.label_db.setText("%.4f" % smp.deltaLab[2])
        # ui.label_de.setText("%.4f" % smp.deltaE)

    def __enableBtn(self, btn):
        """

        :param btn:
        :return:
        """
        btn.setEnabled(True)

    ################################################################################################################
    # 修色窗体；
    ################################################################################################################
    def showColorCorrectDlg(self):
        md = MyDialog()
        ui = Ui_Form_colorCorrect()
        ui.setupUi(md)
        md.setWindowTitle("修色")

        self.__isSetStdSmpAndColorant()

        self.colorCorrectDlg = ui

        idxList = self.getSelectColorant()
        bSelectRight = self.tierManager.selectColorant(idxList)
        if not bSelectRight:
            self.colorCorrectDlg = None
            msgSlot("错误", "请选择三个色浆")
            return

        # 初始化；
        colorantInput = [ui.lineEdit_colorant1, ui.lineEdit_colorant2, ui.lineEdit_colorant3,
                         ui.lineEdit_colorant4, ui.lineEdit_colorant5, ui.lineEdit_colorant6]
        qualityInput = [ui.lineEdit_quality1, ui.lineEdit_quality2, ui.lineEdit_quality3,
                        ui.lineEdit_quality4, ui.lineEdit_quality5, ui.lineEdit_quality6]

        addInput = [ui.lineEdit_add1, ui.lineEdit_add2, ui.lineEdit_add3,
                    ui.lineEdit_add4, ui.lineEdit_add5, ui.lineEdit_add6]

        nCompose = self.tierManager.formula.nComp
        for i in range(nCompose):
            colorantInput[i].setText(self.tierManager.formula.colorantsComp[i].colorants.colorantType)
            self.tierManager.formula.colorantsComp[i].quality = self.cfg.testConc[i]
            qualityInput[i].setText("%.4f" % self.tierManager.formula.colorantsComp[i].quality)

        self.tierManager.colorCorrectFinished.connect(lambda: self.__enableBtn(ui.pushButton_correct))
        ui.pushButton_correct.clicked.connect(lambda: self.__colorCorrectDlgCorrect(ui))
        ui.pushButton_showCompare.clicked.connect(self.__colorCorrectDlgShowCompare)
        ui.pushButton_open.clicked.connect(lambda: self.openRecipeSample(ui.lineEdit_smp))

        if self.cfg.recipeFilePath and os.path.exists(self.cfg.recipeFilePath):
            try:
                self.tierManager.setRecipeSample(self.cfg.recipeFilePath)
                ui.lineEdit_smp.setText(self.cfg.recipeFilePath)
            except:
                print("加载样本光谱失败")
        # 显示结果；
        md.exec()
        self.colorCorrectDlg = None



    def __colorCorrectDlgCorrect(self, ui):
        """
        点击“修色”按钮";
        :param ui:
        :return:
        """
        if not ui.lineEdit_smp.text():
            msgSlot("错误", "请选择配方样本文件")
            return

        msgBoxTmp= QMessageBox()
        ret = msgBoxTmp.information(None, "提示", "请确定有输入原始配方含量", msgBoxTmp.Yes | msgBoxTmp.No, msgBoxTmp.Yes)
        if ret != msgBoxTmp.Yes:
            return

        ui.pushButton_correct.setEnabled(False)

        qualityInput = [ui.lineEdit_quality1, ui.lineEdit_quality2, ui.lineEdit_quality3,
                        ui.lineEdit_quality4, ui.lineEdit_quality5, ui.lineEdit_quality6]
        nCompose = self.tierManager.formula.nComp
        testConc = []
        for i in range(nCompose):
            self.tierManager.formula.colorantsComp[i].quality = float(qualityInput[i].text())
            self.cfg.testConc[i] = self.tierManager.formula.colorantsComp[i].quality
            testConc.append(self.tierManager.formula.colorantsComp[i].quality)
        self.tierManager.startColorCorrect(testConc)

    def __colorCorrectDlgShowCompare(self, ui):
        """
        点击”显示光谱对比“
        :param ui:
        :return:
        """
        testConc = []
        for i in range(self.tierManager.colorCombine.formula.nComp):
            testConc.append(self.tierManager.colorCombine.formula.colorantsComp[i].quality)
        self.tierManager.calcRef(testConc, self.tierManager.group.baseWhite)
        self.tierManager.plotRefCurve()

    ################################################################################################################
    # 评估窗体；
    ################################################################################################################
    def showEvaluateDlg(self):
        md = QDialog()
        ui = Ui_Form_evaluate()
        ui.setupUi(md)
        md.setWindowTitle("评估")

        self.__isSetStdSmpAndColorant()

        idxList = self.getSelectColorant()
        bSelectRight = self.tierManager.selectColorant(idxList, True)
        # if not bSelectRight:
        #     msgSlot("错误", "请选择三个色浆")
        #     return

        # 初始化；
        colorantInput = [ui.lineEdit_colorant1, ui.lineEdit_colorant2, ui.lineEdit_colorant3,
                         ui.lineEdit_colorant4, ui.lineEdit_colorant5, ui.lineEdit_colorant6]

        ui.pushButton_cal.clicked.connect(lambda:self.__calcEavaluate(ui))
        ui.pushButton_show.clicked.connect(self.__showCompare)

        nCompose = self.tierManager.formula.nComp
        for i in range(nCompose):
            colorantInput[i].setText(self.tierManager.formula.colorantsComp[i].colorants.colorantType)
        md.exec()

    def __calcEavaluate(self, ui):
        """
        点击”计算“
        :param ui:
        :return:
        """
        self.tierManager.stdSmp.calcActualRef(self.tierManager.group.K1)
        qualityInput = [ui.lineEdit_quality1, ui.lineEdit_quality2, ui.lineEdit_quality3,
                        ui.lineEdit_quality4, ui.lineEdit_quality5, ui.lineEdit_quality6]
        nCompose = self.tierManager.formula.nComp
        testConc = []
        for i in range(nCompose):
            self.tierManager.formula.colorantsComp[i].quality = float(qualityInput[i].text())
            testConc.append(self.tierManager.formula.colorantsComp[i].quality)
        self.tierManager.calcRef(testConc, self.tierManager.group.baseWhite)

    def __showCompare(self):
        """
        点击”显示光谱对比“
        :return:
        """
        self.tierManager.plotRefCurve()

    ################################################################################################################
    # 获取选择的色浆；
    ################################################################################################################
    def getSelectColorant(self):
        """
        获取选择的色浆列表；
        :return:
        """
        nColorant = self.uiMain.listWidget_colorant.count()
        idxList = []
        for i in range(nColorant):
            if self.uiMain.listWidget_colorant.item(i).checkState():
                idxList.append(i)
        return idxList


    def __isSetStdSmpAndColorant(self):
        if self.uiMain:
            stdSmpText = self.uiMain.lineEdit_stdSmp.text()
            refSmpText =  self.uiMain.lineEdit_ref_curve.text()
            if not stdSmpText:
                msgSlot("警告", "请选择标准样本文件")
            if not refSmpText:
                msgSlot("警告", "请选择梯度光谱数据文件")
